function showTime() {
    var date = new Date()
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()
    if (hour < 10) { hour = "0" + hour }
    if (minute < 10) { minute = "0" + minute }
    if (second < 10) { second = "0" + second }
    var time = year + "年" + month + "月" + day + "日" + hour + ":" + minute + ":" + second
    $("#tim").html(time)
}

function get_c1_data() {
    $.ajax({
        url: "/c1",
        success: function (data) {
            $("#confirm").text(data.confirm)
            $("#confirmNow").text(data.confirm_now)
            $("#heal").text(data.heal)
            $("#dead").text(data.dead)
        }
    })
}


function get_c2_data() {
    $.ajax({
        url: "/c2",
        success: function (data) {
            console.log("/c2")
            console.log(data)
            var glData = data.data

            glData.forEach((item) => {
                if (item.value === 0) {
                    item.height = 1
                }
                if (item.value > 0 && item.value <= 9) {
                    item.height = 2
                }
                if (item.value > 10 && item.value <= 99) {
                    item.height = 3
                }
                if (item.value > 100 && item.value <= 999) {
                    item.height = 4
                }
                if (item.value > 1000 && item.value <= 9999) {
                    item.height = 5
                }
                if (item.value >= 10000) {
                    item.height = 6
                }
                return item;
            })
            option.series[0].data = glData
            myChart.setOption(option);
        },
        error: function (xhr, type, errorThrown) {
        }
    })
}


function get_l1_data() {
    $.ajax({
        url: "/l1",
        success: function (data) {
            console.log("l1:")
            console.log(data.day)

            let chartData = [
                {
                    "当日新增": data.confirm_add,
                },
                {
                    "现有确诊": data.confirm_now,
                },

            ];
            console.log(chartData)

            let colorArr = [
                '#ead656',
                '#fe7c4b',
                '#23b899',
                '#9c6cff',
                '#008ca1',
                '#dca111',
                '#00d2ec',
                '#004aa0',
                '#5e42d2',
                '#387dff',
                '#ead656',
            ];

            let yarrlength = 0;
            let series = [];
            let xarr = data.day


            yarrlength = chartData.length;
            let stationArr = [];
            for (var i = 0; i < yarrlength; i++) {
                stationArr.push({ avgval: [] });
            }

            for (var j = 0; j < yarrlength; j++) {
                for (let key in chartData[j]) {
                    stationArr[j].avgval = chartData[j][key];
                }
            }

            for (var k = 0; k < yarrlength; k++) {
                series.push({
                    name: yarrx[k],
                    type: 'line',
                    symbolSize: 8,
                    data: stationArr[k].avgval,
                    smooth: true,
                    areaStyle: {
                        color: new echarts.graphic.LinearGradient(
                            0,
                            0,
                            0,
                            1, // 变化度
                            // 三种由深及浅的颜色
                            [
                                {
                                    offset: 0,
                                    color: colorArr[k],
                                },
                                {
                                    offset: 0.8,
                                    color: 'rgba(255,255,255,0.1)',
                                },
                                {
                                    offset: 1,
                                    color: 'rgba(0,0,0,0.1)',
                                },
                            ]
                        ),
                        type: 'linear',
                        global: false,
                    },
                });
            }
            let selectedObj = {};

            series.forEach((item, index) => {
                if (index === 0) {
                    selectedObj[item.name] = true;
                } else {
                    selectedObj[item.name] = false;
                }
            });
            ec_left1_Option.series = series;
            ec_left1_Option.legend.selected = selectedObj
            ec_left1_Option.xAxis.data = xarr
            ec_left1_Option.color = colorArr
            ec_left1.setOption(ec_left1_Option)
            ec_left1.hideLoading()

        },
        error: function (xhr, type, errorThrown) {

        }
    })
}

function get_l2_data() {
    $.ajax({
        url: "/l2",
        success: function (data) {
            var update_time = data.update_time
            var details = data.details
            var risk = data.risk
            $("#l2 .ts").html("截至时间：" + update_time)
            var s = ""
            for (var i in details) {
                if (risk[i] === "高风险") {
                    s += "<li><span class='high_risk'>高风险\t\t</span>" + details[i] + "</li>"
                }
                if (risk[i] === "中风险") {
                    s += "<li><span class='middle_risk'>中风险\t\t</span>" + details[i] + "</li>"
                }
                if (risk[i] === "低风险") {
                    s += "<li><span class='low_risk'>低风险\t\t</span>" + details[i] + "</li>"
                }
            }
            $("#risk_wrapper_li1 ul").html(s)
            start_roll()
        },
        error: function (xhr, type, errorThrown) {
        }
    })
}

function get_l3_data() {
    $.ajax({
        url: "/l3",
        success: function (data) {
            ec_left3_Option.series[0].data = data.data
            ec_left3.setOption(ec_left3_Option);
        }
    })

}

function get_r1_data() {
    $.ajax({
        url: "/r1",
        success: function (data) {
            ec_right1_option.yAxis[0].data = data.city;
            ec_right1_option.yAxis[1].data = data.confirm;
            ec_right1_option.series[0].data = data.confirm;
            setInterval(function () {
                // 每次向后滚动一个，最后一个从头开始。
                if (ec_right1_option.dataZoom[0].endValue === data.confirm.length) {
                    ec_right1_option.dataZoom[0].endValue = 4;
                    ec_right1_option.dataZoom[0].startValue = 0;
                    ec_right1_option.dataZoom[1].endValue = 4;
                    ec_right1_option.dataZoom[1].startValue = 0;
                }
                else {
                    ec_right1_option.dataZoom[0].endValue = ec_right1_option.dataZoom[0].endValue + 1;
                    ec_right1_option.dataZoom[0].startValue = ec_right1_option.dataZoom[0].startValue + 1;
                    ec_right1_option.dataZoom[1].endValue = ec_right1_option.dataZoom[1].endValue + 1;
                    ec_right1_option.dataZoom[1].startValue = ec_right1_option.dataZoom[1].startValue + 1;
                }

                ec_right1.setOption(ec_right1_option);
            }, 4000);

        }
    })
}

function get_r2_data() {
    $.ajax({
        url: "/r2",
        success: function (data) {
            console.log(data.kws)
            ec_right2_option.series[0].data = data.kws;
            ec_right2.setOption(ec_right2_option);
        }
    })
}




function get_r3_data() {
    $.ajax({
        url: "/r3",
        success: function (data) {
            var update_time = renderTime(data.update_time)
            var details = data.details
            console.log("details")
            console.log(details)
            $("#r3 .ts").html("截至时间：" + update_time)
            var s = ""
            for (var i in details) {
                s += "<li><div class='fontInner clearfix'><span>" + details[i].cname + "</span><span>" + details[i].confirmAdd + "</span><span>" + details[i].confirm + "</span></div></li>"
            }
            $("#FontScroll ul").html(s)

        },
        error: function (xhr, type, errorThrown) {

        }

    })
}


function get_ec_centert_provinceData() {


    $.ajax({
        url: "/province",
        success: function (res) {
            var areap = res.area //json与地图名关系数据
            var red = convertData(res.sandian, "高风险") //地图红点
            var yellow = convertData(res.sandian, "中风险")//地图黄点
            var nowconfirm = res.data//现确诊（中国地图）
            var addconfirm = res.data2//新增确诊(中国地图)
            var xdata1 = res.confirm//现确诊x轴
            var ydata1 = res.city//现确诊y轴
            var xdata2 = res.confirm2//新增确诊x轴
            var ydata2 = res.city2//新增确诊y轴
            var citydata = res.cityres//各个市区相关数据

            //气泡设置
            var qipao = get_qipao_data("").data
            console.log("qipao")
            console.log(qipao)

            //偏移量
            var offsetData = [
                [80, 53],
                [39, 53],
                [20, 23],
                [10, 73],
                [10, 43],
                [50, 80],
                [63, 35],
                [83, 20],
            ];
            //symbolSize 散点气泡大小
            var symbolSizeData = [100, 85, 75, 92, 87, 78, 77, 95];

            //循环定义series的data值
            var datas = [];
            datas = addqipao(qipao, offsetData, symbolSizeData)
            ec_center_qp_option.series[0].data = datas
            ec_center_qp.setOption(ec_center_qp_option);
            // console.log(ec_center_qp_option.series[0].data)



            ec_center_option.series[0].data = nowconfirm
            ec_center_option.series[1].data = red
            ec_center_option.series[2].data = yellow
            ec_center.setOption(ec_center_option);

            ec_center_bar_option.title[0].text = "现确诊人数"
            ec_center_bar_option.yAxis.data = ydata1
            ec_center_bar_option.series[0].data = xdata1
            ec_center_bar.setOption(ec_center_bar_option);



            showCity("../static/constant/china.json")
            //滚动条定时器
            setInterval(function () {
                // 每次向后滚动一个，最后一个从头开始。
                if (ec_center_bar_option.dataZoom[0].endValue === xdata1.length) {
                    ec_center_bar_option.dataZoom[0].endValue = 7;
                    ec_center_bar_option.dataZoom[0].startValue = 0;
                }
                else {
                    ec_center_bar_option.dataZoom[0].endValue = ec_center_bar_option.dataZoom[0].endValue + 1;
                    ec_center_bar_option.dataZoom[0].startValue = ec_center_bar_option.dataZoom[0].startValue + 1;
                }

                ec_center_bar.setOption(ec_center_bar_option);
            }, 5000);

            //地图下钻
            ec_center.on('click', function (param) {
                if (param.componentSubType === 'map') {
                    //出现返回按钮
                    $('#box').css('display', 'block');
                    $("#box-title").html('返回');
                    // 禁止选择
                    $("input:radio[name='confirmdata'][value='" + "2" + "']").attr("checked", true).prop("checked", true);
                    $("input:radio[name='confirmdata']:not(:checked)").attr("disabled", true).prop("disabled", true);

                    var cityshuju = []
                    for (var j = 0, len = citydata.length; j < len; j++) {
                        if (citydata[j].province == param.name) {
                            cityshuju.push({
                                name: citydata[j].city,
                                value: citydata[j].confirm_add,
                            })
                        }
                    }

                    //进行下钻
                    for (var i = 0, len = areap.length; i < len; i++) {
                        if (param.name == areap[i].cname) {
                            if (areap[i].fid === 1) {
                                //显示省级地图
                                console.log("开始下钻省")
                                showCity(areap[i].url)
                                //显示省级散点
                                ec_center_option.series[1].data = downconvertData(res.sandian, param.name, "高风险")
                                ec_center_option.series[2].data = downconvertData(res.sandian, param.name, "中风险")
                                ec_center.setOption(ec_center_option);
                                //显示对应气泡
                                var q = get_qipao_data(param.name).data
                                var d = addqipao(q, offsetData, symbolSizeData)
                                ec_center_qp_option.series[0].data = d
                                ec_center_qp.setOption(ec_center_qp_option);
                                //显示本土新增数据(地图)
                                ec_center_option.series[0].data = cityshuju
                                ec_center_option.series[0].name = "本土新增确诊人数"
                                ec_center.setOption(ec_center_option);
                                //显示本土新增数据(柱形图)
                                ec_center_bar_option.series[0].data = xdata2
                                ec_center_bar_option.yAxis.data = ydata2
                                ec_center_bar_option.title[0].text = "新增确诊人数"
                                ec_center_bar.setOption(ec_center_bar_option);
                            }
                            if (areap[i].url == null) {

                                //已经进入省级地图，开始打开高德
                                var gd = document.getElementById("gd");
                                // gd.style.display = 'block';//打开高德地图
                                gd.style.visibility = 'visible'//打开高德地图
                                //向高德地图iframe传递参数
                                var data = {
                                    area: param.name
                                }
                                var gdframe = document.getElementById('gdframe');
                                gdframe.contentWindow.postMessage(data, '*')
                            }
                        }
                    }



                }
            })

            //地图回钻以及显示对应地方散点
            $('#box').click(function () {
                $('#box').hide();
                showCity("../static/constant/china.json")
                //显示国家散点
                ec_center_option.series[1].data = convertData(res.sandian, "高风险")
                ec_center_option.series[2].data = convertData(res.sandian, "中风险")
                //显示国家现确诊数据(地图)
                ec_center_option.series[0].data = nowconfirm
                ec_center_option.series[0].name = "现确诊人数"
                ec_center.setOption(ec_center_option);
                //多选重置到现确诊人数
                $("input:radio[name='confirmdata']").removeAttr("disabled").removeProp("disabled");
                $("input:radio[name='confirmdata'][value='" + "1" + "']").attr("checked", true).prop("checked", true);
                //显示气泡
                ec_center_qp_option.series[0].data = datas
                ec_center_qp.setOption(ec_center_qp_option);
                //显示国家现确诊数据(柱形图)
                ec_center_bar_option.series[0].data = xdata1
                ec_center_bar_option.yAxis.data = ydata1
                ec_center_bar_option.title[0].text = "现确诊人数"
                ec_center_bar.setOption(ec_center_bar_option);
            })

            //现确诊和新增确诊地图切换
            $(document).ready(function () {
                $('input[type=radio][name=confirmdata]').change(function () {
                    console.log($(this).val())
                    if ($(this).val() == 1) {
                        console.log("切换为现确诊")
                        ec_center_option.series[0].data = nowconfirm
                        ec_center.setOption(ec_center_option);
                        ec_center_bar_option.series[0].data = xdata1
                        ec_center_bar_option.yAxis.data = ydata1
                        ec_center_bar_option.title[0].text = "现确诊人数"
                    } else {
                        console.log("切换为新增确诊")
                        ec_center_option.series[0].data = addconfirm
                        ec_center.setOption(ec_center_option);
                        ec_center_bar_option.series[0].data = xdata2
                        ec_center_bar_option.yAxis.data = ydata2
                        ec_center_bar_option.title[0].text = "新增确诊人数"
                    }

                });
                ec_center_bar.setOption(ec_center_bar_option);
            });



        }
    })
}


function get_qipao_data(province) {
    var result = "";
    $.ajax({
        url: '/qipao',
        type: "get",
        data: {
            "province": province
        },
        async: false,
        success: function (data) {
            result = data;
        }

    });
    return result;
}

function get_fudiao_data() {
    $.ajax({
        url: '/fudiao',
        success: function (data) {
            console.log("get_fudiao_data:::")
            console.log(data)
            console.log(data.confimAdd)
            console.log(data.highRiskAreaNum)
            console.log(data.mediumRiskAreaNum)
            console.log(data.wzzAdd)
            var s1 = "<span>" + data.confimAdd + "</span><p>当日新增</p>"
            var s2 = "<span>" + data.highRiskAreaNum + "</span><p>高风险地区</p>"
            var s3 = "<span>" + data.mediumRiskAreaNum + "</span><p>中风险地区</p>"
            var s4 = "<span>" + data.wzzAdd + "</span><p>无症状感染</p>"
            $(".cicle8").html(s1)
            $(".cicle9").html(s2)
            $(".cicle10").html(s3)
            $(".cicle11").html(s4)
        }

    });
}



showTime()
get_c1_data()
get_c2_data()
get_l1_data()
get_l2_data()
get_l3_data()
get_r1_data()
get_r2_data()
get_r3_data()
get_fudiao_data()
get_ec_centert_provinceData()


setInterval(showTime, 1000) //1秒调用1次
setInterval(get_c1_data(), 1000 * 60)
setInterval(get_c2_data(), 1000 * 60)
setInterval(get_l1_data(), 1000 * 60)
setInterval(get_l2_data(), 1000 * 60)
setInterval(get_l3_data(), 1000 * 60)
setInterval(get_r1_data(), 1000 * 60)
setInterval(get_r2_data(), 1000 * 3600)
setInterval(get_r3_data(), 1000 * 60)
setInterval(get_fudiao_data(), 1000 * 60)
setInterval(get_ec_centert_provinceData(), 1000 * 60)
